//
//  Message.m
//  SouthChina
//
//  Created by creator_lq on 14/12/4.
//  Copyright (c) 2014年 科创. All rights reserved.
//

#import "Message.h"

@implementation Message


@synthesize mID;
@synthesize receiverId;
@synthesize sequences;
@synthesize user_id;
@synthesize senderId;
@synthesize messageType;
@synthesize mediaType;
@synthesize state;
@synthesize length;
@synthesize mesSesion_sess_id;
@synthesize ut_id;
@synthesize text;


-(Message*)initWithData:(NSDictionary*)msgMap msgId:(NSString*)msgid updateTime:(NSDate*) time{
    return [self initWithData:msgMap msgId:msgid updateTime:time seesionID:nil];
}

-(Message*)initWithData:(NSDictionary*)msgMap msgId:(NSString*)msgid updateTime:(NSDate*) time seesionID:(NSString*)sessId;
{
    
    NSMutableDictionary *map = [[NSMutableDictionary alloc] initWithDictionary:msgMap];
    mID= msgid;
    receiverId= [map objectForKey:RECEIVEID];
    sequences= [map objectForKey:SEQUENCES];
    user_id= [map objectForKey:USER_ID];
    senderId= [map objectForKey:SENDERID];
    messageType= [map objectForKey:MESSSAGETYPE];
    mediaType= [map objectForKey:MEDIATYPE];
    state= [map objectForKey:STATE];
    length= [map objectForKey:LENGTH];
    mesSesion_sess_id= sessId;
    ut_id= [map objectForKey:UT_ID];
    text= [map objectForKey:TEXT];
 
    [map removeObjectForKey:RECEIVEID];
    [map removeObjectForKey:SEQUENCES];
    [map removeObjectForKey:USER_ID];
    [map removeObjectForKey:SENDERID];
    [map removeObjectForKey:MESSSAGETYPE];
    [map removeObjectForKey:MEDIATYPE];
    [map removeObjectForKey:STATE];
    [map removeObjectForKey:LENGTH];
    [map removeObjectForKey:UT_ID];
    [map removeObjectForKey:TEXT];
    
    [map removeObjectForKey:MSG_ID];
    [map removeObjectForKey:@"update_time"];
    [map removeObjectForKey:@"sendTime"];

 
    self.otherInfo = [map JSONString];
    
    if (time) {
        self.upate_time = time;
    }else{
        if([map objectForKey:@"sendTime"]){
            self.upate_time = [[NSString stringWithFormat:@"%@",[map objectForKey:@"sendTime"]] dateFromString];

        }else if([map objectForKey:@"update_time"]){
            self.upate_time = [[NSString stringWithFormat:@"%@",[map objectForKey:@"update_time"]] dateFromString];

        }else{
            self.upate_time = [NSDate date];
        }
        

    }
    
    
    return  self;

    
}

-(BOOL)isSendMine{
    return [senderId isEqualToString:receiverId];
}

-(BOOL)isMine{
    
    return [senderId isEqualToString:DE.userId];
}


@end
